package com.chenglin.picturepicker

import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import androidx.navigation.ui.AppBarConfiguration
import androidx.recyclerview.widget.GridLayoutManager
import com.chenglin.picturepicker.adapter.ImageAdapter
import com.chenglin.picturepicker.adapter.SelectPhotoActivity
import com.chenglin.picturepicker.bean.MediaBean
import com.chenglin.picturepicker.databinding.ActivityMainBinding
import com.google.android.material.snackbar.Snackbar
import java.io.File


class MainActivity : AppCompatActivity() {


    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityMainBinding

    private lateinit var mCurrentPhotoPath :String

    var newMediaBeen = arrayListOf<MediaBean>()
    var mediaBeen = arrayListOf<MediaBean>()
    val allPhotosTemp: HashMap<String, ArrayList<MediaBean>> = HashMap() //所有照片

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.rv.layoutManager = GridLayoutManager(this,3)
        binding.rv.adapter = ImageAdapter(this,newMediaBeen,object : ImageAdapter.ImageAdapterCallBack{
            override fun onClickItem(position: Int, bean: MediaBean) {
                binding.rv.adapter?.notifyItemChanged(position)
            }

            override fun onClickTag(position: Int, bean: MediaBean) {

            }

        })
        binding.btnScan.setOnClickListener {
        }

        var registerForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
            it?.run {
                val extras = it.data?.extras
                if (extras != null){
                    val list = extras?.get("selectList") as ArrayList<MediaBean>
                    newMediaBeen.clear()
                    newMediaBeen.addAll(list)
                    binding.rv.adapter?.notifyDataSetChanged()
                }

            }

        }

        //获取权限
        var registerPermission : ActivityResultLauncher<Array<String>> = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()){
           it?.run {
               if (it[Manifest.permission.CAMERA] == true && it[Manifest.permission.READ_EXTERNAL_STORAGE] ==true){
                   var intent = Intent(this@MainActivity, SelectPhotoActivity::class.java)
                   registerForResult.launch(intent)
               }else{
                   Snackbar.make(binding.root,"请打开权限",Snackbar.LENGTH_LONG).show()
               }

           }

        }

        binding.btnStart.setOnClickListener {
            registerPermission.launch(arrayOf(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE))

        }


        binding.btnOpenCamera.setOnClickListener {
            var mediaStorageDir =  File(externalCacheDir?.path+"/TuXuanXuan");
            if (!mediaStorageDir.exists()) {
                mediaStorageDir.mkdirs()
            }
            val mediaFile  = File(mediaStorageDir.path + File.separator + "${System.currentTimeMillis()}.png")
            mCurrentPhotoPath = mediaFile.absolutePath;
            //小于7.0版本
            var uriForFile :Uri= if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
                Uri.fromFile(mediaFile )
            }else{
                // 7.0 利用FileProvider
                val authority: String = "$packageName.fileprovider" // 需要在配置文件中配置
                FileProvider.getUriForFile(this, authority, mediaFile )
            }
            var intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            // 添加权限
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
            intent.putExtra(MediaStore.EXTRA_OUTPUT,uriForFile)
            startActivityForResult(intent,2001)
        }
    }








}